package com.hruiworks.campusgroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hruiworks.campusgroup.common.PageVO;
import com.hruiworks.campusgroup.enums.ErrorCode;
import com.hruiworks.campusgroup.exception.BusinessException;
import com.hruiworks.campusgroup.pojo.entity.ActivityGrade;
import com.hruiworks.campusgroup.pojo.entity.Feedback;
import com.hruiworks.campusgroup.pojo.entity.User;
import com.hruiworks.campusgroup.pojo.request.FeedbackCreateRequest;
import com.hruiworks.campusgroup.service.FeedbackService;
import com.hruiworks.campusgroup.dao.mapper.FeedbackMapper;
import com.hruiworks.usercheck.util.ParamUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author jiushui
* @description 针对表【feedback(反馈)】的数据库操作Service实现
* @createDate 2025-03-26 19:13:20
*/
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback>
    implements FeedbackService{

    @Resource
    private FeedbackMapper feedbackMapper;

    @Override
    public Boolean submitFeedback(FeedbackCreateRequest feedbackCreateRequest, User user) {
        if (ParamUtils.isAnyBlank(feedbackCreateRequest)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不全");
        }
        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(feedbackCreateRequest, feedback);
        feedback.setUserId(user.getId());
        int insert = feedbackMapper.insert(feedback);
        if (insert != 1) {
            throw new BusinessException(ErrorCode.OTHER_ERROR, "数据库插入出错");
        }
        return true;
    }

    @Override
    public PageVO<Feedback> pageFeedback(Long current, Long pageSize, Long activityId) {
        if (ParamUtils.isAnyBlank(activityId, current, pageSize)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数有误或不全");
        }

        PageVO<Feedback> feedbackPageVO = new PageVO<>();

        Page<Feedback> queryFeedbackPage = new Page<>(current, pageSize);
        LambdaQueryWrapper<Feedback> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Feedback::getActivityId, activityId);
        lambdaQueryWrapper.orderByDesc(Feedback::getId);

        Page<Feedback> activityGradePage = feedbackMapper.selectPage(queryFeedbackPage, lambdaQueryWrapper);


        feedbackPageVO.setRecords(activityGradePage.getRecords());
        feedbackPageVO.setTotal(activityGradePage.getTotal());
        feedbackPageVO.setSize(activityGradePage.getSize());
        feedbackPageVO.setCurrent(activityGradePage.getCurrent());

        return feedbackPageVO;

    }
}




